package it.canraid.leaflet.control;

import it.canraid.leaflet.R;
import it.canraid.leaflet.control.activity.DrawActivity;
import it.canraid.leaflet.view.FlingPanel;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnTouchListener;

public class EraseRestoreListener implements OnTouchListener {
    private int readyId = 0;
    private int pressId = 0;
    private DrawActivity context;

    public EraseRestoreListener(DrawActivity context, View v, int readyId, int pressId) {
	this.readyId = readyId;
	this.pressId = pressId;
	this.context = context;

	v.setOnTouchListener(this);
	v.setBackgroundResource(readyId);
	v.setEnabled(true);
    }

    public boolean onTouch(View v, MotionEvent event) {
	switch (event.getAction()) {

	case MotionEvent.ACTION_DOWN:
	    v.setBackgroundResource(pressId);
	    FlingPanel bodyPanelEditable = (FlingPanel) context.bodyPanel;
	    if (v.getId() == R.id.eraseImage) {
		int erase = 0;
		int restore = 0;
		if (bodyPanelEditable.getActualId() == Kernel.ID_DRAW_VIEW) {
		    erase = Kernel.R().eraseLines();
		    restore = Kernel.R().getActiveEraseRestore(Kernel.ID_DRAW_VIEW, R.id.restoreImage);
		} else if (bodyPanelEditable.getActualId() == Kernel.ID_TEXT_VIEW) {
		    erase = Kernel.R().eraseWords();
		    restore = Kernel.R().getActiveEraseRestore(Kernel.ID_TEXT_VIEW, R.id.restoreImage);
		}
		if (restore > 0) {
		    context.restoreButton.setEnabled(true);
		    context.restoreButton.setAlpha(1f);
		} else {
		    context.restoreButton.setEnabled(false);
		    context.restoreButton.setAlpha(Kernel.ALPHA_DISABLE_VIEW);
		}
		if (erase > 0) {
		    v.setAlpha(1f);
		} else {
		    v.setAlpha(Kernel.ALPHA_DISABLE_VIEW);
		}
	    } else if (v.getId() == R.id.restoreImage) {
		int erase = 0;
		int restore = 0;
		if (bodyPanelEditable.getActualId() == Kernel.ID_DRAW_VIEW) {
		    erase = Kernel.R().restoreLines();
		    restore = Kernel.R().getActiveEraseRestore(Kernel.ID_DRAW_VIEW, R.id.restoreImage);
		} else if (bodyPanelEditable.getActualId() == Kernel.ID_TEXT_VIEW) {
		    erase = Kernel.R().restoreWords();
		    restore = Kernel.R().getActiveEraseRestore(Kernel.ID_TEXT_VIEW, R.id.restoreImage);
		}
		if (restore > 0) {
		    v.setAlpha(1f);
		} else {
		    v.setAlpha(Kernel.ALPHA_DISABLE_VIEW);
		}
		if (erase > 0) {
		    context.eraseButton.setEnabled(true);
		    context.eraseButton.setAlpha(1f);
		} else {
		    context.eraseButton.setEnabled(false);
		    context.eraseButton.setAlpha(Kernel.ALPHA_DISABLE_VIEW);
		}
	    }

	    if (bodyPanelEditable.getActualId() == Kernel.ID_DRAW_VIEW)
		context.drawBody.invalidate();
	    else if (bodyPanelEditable.getActualId() == Kernel.ID_TEXT_VIEW)
		context.textBody.invalidate();

	    break;
	case MotionEvent.ACTION_UP:
	    v.setBackgroundResource(readyId);
	    if (v.getAlpha() < 1f) {
		v.setEnabled(false);
	    } else {
		v.setEnabled(true);
	    }
	    break;
	}
	return true;
    }

}
